package com.yuven.appframework.manager

import android.content.Context
import android.content.pm.PackageInfo
import com.yuven.appframework.BuildConfig

object PackageInfoServer {

    fun getPackageInfo(context:Context): PackageInfo? {
        val pm = context.packageManager
        try{
            var packageInfo = pm.getPackageInfo(context.packageName,0)
            return packageInfo
        }catch (e:Exception){
            e.printStackTrace()
        }
        return null
    }

    fun getPackageName(context: Context): String? {
        return getPackageInfo(context)?.packageName
    }

    fun getVersionName(context: Context):String?{
        return getPackageInfo(context)?.versionName
    }

    fun getVersionCode(context: Context): Int? {
        return getPackageInfo(context)?.versionCode
    }

    fun getAppName(context: Context):String?{
        try {
            val packageInfo = getPackageInfo(context)
            val applicationInfo = packageInfo?.applicationInfo
            val labelRes = applicationInfo?.labelRes
            //返回App的名称
            return context.resources.getString(labelRes!!)
        }catch (e:Exception){
            e.printStackTrace()
        }
        return "null"
    }

    fun isDebugPackage():Boolean{
        return BuildConfig.DEBUG
    }

}
